package com.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间计算类
 */
public class TimeUtils {
    /**
     * 友好时间显示
     * @param time
     * @return
     */
    public static String friendlyTime(Date time) {
        //获取time距离当前的秒数
        int ct = (int) ((System.currentTimeMillis() - time.getTime()) / 1000);
        if (ct == 0) {
            return "刚刚";
        }
        if (ct > 0 && ct < 60) {
            return ct + "秒前";
        }
        if (ct >= 60 && ct < 3600) {
            return Math.max(ct / 60, 1) + "分钟前";
        }
        if (ct >= 3600 && ct < 86400)
            return ct / 3600 + "小时前";
        if (ct >= 86400 && ct < 2592000) { //86400 * 30
            int day = ct / 86400;
            return day + "天前";
        }
        if (ct >= 2592000 && ct < 31104000) { //86400 * 30
            return ct / 2592000 + "月前";
        }
        return ct / 31104000 + "年前";
    }

    public static String getTime() {
        return String.valueOf(System.currentTimeMillis());
    }

    public static String TimeStampDate(String timestampString){
        Long timestamp = Long.parseLong(timestampString);
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(timestamp));
        return date;
    }

    public static String getDateTimeStr(Long timestamp){
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
        return date;
    }

    public static String getData(String timestampString){
        Long timestamp = Long.parseLong(timestampString);
        String date = new SimpleDateFormat("MM月dd日").format(new Date(timestamp));
        return date;
    }
    public static String getAllData(String timestampString){
        Long timestamp = Long.parseLong(timestampString);
        String date = new SimpleDateFormat("MM月dd日 HH:mm").format(new Date(timestamp));
        return date;
    }


    /*将字符串转为时间戳*/
    public static long getStringToDate(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try{
            date = sdf.parse(time);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 08:30
     * @param time
     * @return
     */
    public static long getRemainingTime(String time){
        String[] split = time.split(":");
        String s = TimeUtils.TimeStampDate(TimeUtils.getTime());
        s =  s + " " +  split[0] + ":" + split[1] + ":" + "00";
        long stringToDate = getStringToDate(s);
        long sum = stringToDate - System.currentTimeMillis();
        return sum;
    }



    /**
     * 返回开始时间和结束时间之间的时间差(毫秒)
     * @param time		格式: "08:00-20:00"
     * @return
     */
    public static long getStartAndEndTimeGap(String time){
        String[] split = time.split("-");

//		split[0] = split[0].substring(0,split[0].indexOf(":")) + split[0].substring(split[0].indexOf(":") + 1,split[0].length());
//		split[1] = split[1].substring(0,split[1].indexOf(":")) + split[1].substring(split[1].indexOf(":") + 1,split[1].length());
//		int startTime = Integer.valueOf(split[0]);
//		int endTime = Integer.valueOf(split[1]);

//		return (endTime - startTime)  * 60 * 1000;
        return getRemainingTime(split[1]) - getRemainingTime(split[0]);
    }

    /**
     * 获得指定日期的后一天
     *
     * @return
     */
    public static String getSpecifiedDayAfter(Date date, int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + i);
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH) + 1;
        int d = c.get(Calendar.DAY_OF_MONTH);
        String dayAfter = y + "-" + m + "-" + d;
        return dayAfter;
    }


    /**
     * 返回今天的日期
     * @return
     */
    public static String getTodayDate(){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        return sf.format(c.getTime());
    }

    /**
     * 判断日期是不是在指定周内,格式 "2016-5-12"
     * @param day
     * @param week
     * @return
     */
    public static boolean isInThisWeek(String day, String week){
//		List<String>list = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date sunday = new Date();
        try {
            sunday = simpleDateFormat.parse(week);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 7; i++) {
            String specifiedDayAfter = getSpecifiedDayAfter(sunday, i);
            String[] split = specifiedDayAfter.split("-");
            split[1] = split[1].length() == 1 ? "0" + split[1] : split[1];
            split[2] = split[2].length() == 1 ? "0" + split[2] : split[2];
//			list.add(split[0] + "-" + split[1] + "-" + split[2]);
            boolean a =  (day.equals(split[0] + "-" + split[1] + "-" + split[2] + " 00:00:00"));

            if (a)return true;
        }
        return false;
    }


    /**
     * 返回上周的日期开始到结束日期
     * true 上周开始时间  false 上周结束时间
     * @return
     */
    public static String StringData(boolean b){
        int mYear;
        int mMonth;
        int mNowDay;
        int mNextDay;
        String m_month;
        String m_day;
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        Date date = new Date();
        c.setTime(date);
        int todayOfWeek = (c.get(Calendar.DAY_OF_WEEK)) - 1;
        if (!b)todayOfWeek =  todayOfWeek % 7 + 1;
        else todayOfWeek = todayOfWeek % 7 + 7;


        // 这边定义的年月日变量都要int类型 ， 不要问我为什么
        mYear = c.get(Calendar.YEAR); // 获取当前年份
        mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        mNowDay = c.get(Calendar.DAY_OF_MONTH);// 获取当前月份的日期号码

        c.set(Calendar.DAY_OF_MONTH, mNowDay - todayOfWeek); // 延后3天
        mNextDay = c.get(Calendar.DAY_OF_MONTH);
        // 判断延后的日期小于今天的日期，月份加一
        if(mNowDay - mNextDay < 1) mMonth -= 1;
        // 判断延后的月份大于本月的月份，月份设置为一月份，年份加一
        if(mMonth>12){
            mMonth = 1;
            mYear +=1 ;
        }else if (mMonth<1){
            mMonth = 12;
            mYear -= 1;
        }
        // 测试今天的日期

        // 如果 月份为个位数则加个0在前面
        if(mMonth < 10){
            m_month = "0" + mMonth ;
        } else{
            m_month = "" + mMonth ;
        }
        // 如果 天数为个位数则加个0在前面
        if(mNextDay < 10){
            m_day = "0" + mNextDay ;
        }else{
            m_day = "" + mNextDay ;
        }
        return mYear + "-" + m_month + "-" + m_day + " 00:00:00";
    }
}
